<?php
class parseTree
{
	var $ads_type_file = "";
	var $isX = false;
	var $rowSize=2;
	function parseTree()
	{
		$this->ads_type_file = CACHE_PATH."ads_type_arr.data.php";
		$this->ads_city_file = CACHE_PATH."ads_city_arr.data.php";
	}
	function make_ads_type()
	{
			$db = Factory::getDB("ads_type");
			$tree =Factory::getItemClass('treeCache');
			$tree->set($db);
			$tree->tableName=$db->tableName;
			$tree->pidStr ="ads_type_pid";
			$tree->idStr = "ads_type_id";
			$tree->title = "ads_type_name";
			$tree->createArrayName ="ads_type_arr";
			$tree->where = " ads_type_state=1 ORDER BY ads_type_id DESC ";
			$tree->appendArr = array("ads_type_info");
			$tree->createFileName = $this->ads_type_file;
			$tree->is_utf8 = true;
			return $tree->make();
	}
	function get_ads_type_str()
	{
		$temp_arr = $this->get_ads_type_arr();
		$treeArr = empty($temp_arr) ? array() : $temp_arr;
		$rootStr = isset($temp_arr['root']) ? $temp_arr['root'] : "";
		$show_content = $this->__parseTree($treeArr,$rootStr,'pares_type_link');
		return $show_content;
	}
	function get_ads_type_arr()
	{
		return is_file($this->ads_type_file) ? require($this->ads_type_file) : array();
	}
	function pares_type_link($info)
	{
			$class_name = "ads_type";
			$tempStr = "[<a href='?c={$class_name}&m=add&ads_type_pid={$info['id']}'>子-添加</a>]";
			$tempStr .="[<a href='?c={$class_name}&m=edit&ads_type_id={$info['id']}'>编辑</a>]";
			$tempStr .="[<a href='?c={$class_name}&m=del&ads_type_id={$info['id']}'>删除</a>]";
			return $tempStr;
	}
	//根据提供的类别号取得类别数组
	function get_type_arr($type_no=0)
	{
		$temp_arr = $this->get_ads_type_arr();
		$rootStr = $type_no==0 ? (isset($temp_arr['root'])?$temp_arr['root'] : "") : (isset($temp_arr[$type_no]['cid'])?$temp_arr[$type_no]['cid']:""); 
		$temp = explode(',',$rootStr);
		$return_temp = array();
		foreach($temp as $cid)
		{
			if(isset($temp_arr[$cid])) $return_temp[$temp_arr[$cid]['id']]=$temp_arr[$cid]['title'];
		}
		return $return_temp;
	}

	function make_ads_city()
	{
			$db = Factory::getDB("ads_city");
			$tree =Factory::getItemClass('treeCache');
			$tree->set($db);
			$tree->tableName=$db->tableName;
			$tree->pidStr ="ads_city_pid";
			$tree->idStr = "ads_city_no";
			$tree->title = "ads_city_name";
			$tree->createArrayName ="ads_city_arr";
			$tree->where = " ads_city_state=1 ORDER BY ads_city_no DESC ";
			$tree->appendArr = array("ads_city_info");
			$tree->createFileName = $this->ads_city_file;
			$tree->is_utf8 = true;
			return $tree->make();
	}
	function get_ads_city_arr()
	{
		return is_file($this->ads_city_file) ? require($this->ads_city_file) : array();
	}
	function get_ads_city_str()
	{
		$temp_arr = $this->get_ads_city_arr();
		$treeArr = empty($temp_arr) ? array() : $temp_arr;
		$rootStr = isset($temp_arr['root']) ? $temp_arr['root'] : "";
		$show_content = $this->__parseTree($treeArr,$rootStr,'pares_city_link');
		return $show_content;
	}
	function pares_city_link($info)
	{
			$class_name = "ads_city";
			$tempStr = "[<a href='?c={$class_name}&m=add&ads_city_pid={$info['id']}'>子-添加</a>]";
			$tempStr .="[<a href='?c={$class_name}&m=edit&ads_city_no={$info['id']}'>编辑</a>]";
			$tempStr .="[<a href='?c={$class_name}&m=del&ads_city_no={$info['id']}'>删除</a>]";
			return $tempStr;
	}
	//根据提供的城市号取得城市数组
	function get_city_arr($city_no=0)
	{
		$temp_arr = $this->get_ads_city_arr();
		if($city_no === 0)
		{
			$rootStr  = isset($temp_arr['root'])?$temp_arr['root'] : "";
		}else{
			$rootStr = isset($temp_arr[$city_no]) ? $temp_arr[$city_no]['cid'] : "";
		}
		$temp = explode(',',$rootStr);
		$return_temp = array();
		foreach($temp as $cid)
		{
			if(isset($temp_arr[$cid])) $return_temp[$temp_arr[$cid]['id']]=$temp_arr[$cid]['title'];
		}
		return $return_temp;
	}

	function  __parseTree($treeCache,$rootStr,$fuc_str)
		{
			$tempStr = "";
			$temp = explode(',',$rootStr);
			if(empty($temp)) return "";
			$this->layer=0;
			foreach ($temp AS $cid)
			{
				if(isset($treeCache[$cid]))
				{
					$info = $treeCache[$cid];
					$cidStr = $info['cid'];
					//如果下面有子id
					if($info["cid"]!="")
					{
						$tempStr .="<div >";
						$tempStr .= str_repeat('&nbsp;',($info['depth']-1)*3);
						if($info["cid"]!="")
						{	
							//$tempStr .="<img src='/images/arrow.jpg'  style='cursor:hand;' onClick='javascript:ShowMenu(\"l_{$info['id']}\");' id='pic_l_{$info['id']}'>";
						}
						//$tempStr .=">";
						$tempStr.=$info['title'];
						$tempStr .=$this->{$fuc_str}($info);
						$tempStr .="</div>";
						$tempStr .="<div id='l_{$info['id']}' >";
						$tempStr .= $this->__parseTree($treeCache,$info['cid'],$fuc_str);
						$tempStr .="</div>";
					}else{
						$this->layer++;
						$tempStr .= str_repeat('&nbsp;',($info['depth']-1)*3);
						$tempStr.=$info['title'];
						$tempStr .=$this->{$fuc_str}($info);
						if($this->isX==true)
						{
							if($this->layer % $this->rowSize ==0)
								$tempStr .= "<br/>";
							else
								$tempStr .= " &nbsp;&nbsp; ";
						}else{
							$tempStr .= "<br/>";
						}
					}
				}
			}
			return $tempStr;
		}
}
?>